package com.jiahao.material.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiahao.material.entity.Permission;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

public interface PermissionMapper extends BaseMapper<Permission> {

    @Results(id = "permissionMap" ,value={
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "type",property = "type"),
            @Result(column = "code",property = "code"),
            @Result(column = "name",property = "name"),
            @Result(column = "description",property = "description"),
            @Result(column = "createTime",property = "createTime"),
            @Result(column = "updateTime",property = "updateTime"),
            @Result(column = "status",property = "status")
    })
    @Select("select * from permission where id in (select pid from role_permission where rid = #{rid})")
    public List<Permission> getPermissionsByRid(String rid);

    @ResultMap("permissionMap")
    @Select({
            "<script>",
            "select * from permission",
            "<where>",
            "<if test='map.name != null and map.name!=\"\"'>and name like concat(concat('%',#{map.name},'%'))</if>",
            "<if test='map.code != null and map.code!=\"\"'>and code like concat(concat('%',#{map.code},'%'))</if>",
            "<if test='map.type != null and map.type!=\"\"'>and type = #{map.type}</if>",
            "<if test='map.status != null and map.status!=\"\"'>and status = #{map.status}</if>",
            "</where>",
            "</script>"
    })
    public IPage<Permission> getByPage(Page<Permission> page, @Param("map")Map<String,Object> map);
}
